package com.moli.iov.handle.real.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.base.model.dto.req.VehicleTboxReqDto;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.base.remote.IVehicleTboxRemoteService;
import com.moli.iov.handle.real.service.IVehicleService;
import com.moli.iov.protocol.model.VehicleRegisterReq;
import com.moli.iov.protocol.model.dto.VehicleOnLineDto;
import com.moli.iov.real.model.res.VehicleStatusDto;
import com.moli.iov.real.service.IRealService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.task.model.dto.VehicleBaseDto;
//import com.moli.iov.task.service.request.IVehicleBaseRequestService;
//import com.moli.iov.vehicle.model.dto.req.VehicleTboxAddReqDto;
//import com.moli.iov.vehicle.model.dto.req.VehicleTboxReqDto;
//import com.moli.iov.vehicle.service.IVehicleTboxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class VehicleServiceImpl implements IVehicleService {

    @Resource
    IVehicleBaseRemoteService iVehicleBaseRemoteService;

    @Resource
    IVehicleTboxRemoteService vehicleTboxService;

    @Resource
    IRealService realService;

//    @Resource
//    IVehicleBaseRequestService vehicleBaseRequestService;

    @Override
    public VehicleStatusDto getOdoByVin(String vin){
        if(StringUtils.isEmpty(vin)){
            log.error("查询车辆里程，车架号不能为空");
            return null;
        }
        List<String> list = new ArrayList<>();
        list.add(vin);
        RestResponse<Map<String, VehicleStatusDto>> res = realService.getVehicleStatusByVin(list);
        if(res!=null && res.getData()!=null && res.getData().size()>0){
            return res.getData().get(vin);
        }
        return null;
    }


    @Override
    public Boolean registerVehicle(VehicleRegisterReq registerReq) {

//        VehicleTbox tbox = new VehicleTbox();
        //设置iccid
        VehicleTboxReqDto tbox = new VehicleTboxReqDto();
        tbox.setIccid(registerReq.getIccid());
        //设置车架号
        tbox.setVin(registerReq.getVin());
        //设置设备编号
        tbox.setDeviceId(registerReq.getDeviceNum());

        tbox.setRegisterTime(registerReq.getRegisterDate() == null ? null : registerReq.getRegisterDate().getDate());

//        VehicleTboxAddDto dto = new VehicleTboxAddDto();
//        dto.add(tbox);
        try {

//            VehicleTboxAddReqDto vtDto = new VehicleTboxAddReqDto();
//            vtDto.add(tbox);
            RestResponse result = vehicleTboxService.gatewayRegisterVehicleTbox(tbox);
//            if (result == null ? false : (result.code == 0 ? true : false)) {
//                VehicleBaseDto dto = new VehicleBaseDto();
//                dto.setVin(registerReq.getVin());
//                try{
//                    vehicleBaseRequestService.notifyOp(dto);
//                }catch (Exception e1){
//                    log.error("**发起同步任务异常** {}", e1.getMessage());
//                }
//            }
            return (result!=null && result.flag);
        } catch (Exception e) {
            log.error("**新增车辆服务异常** {}",e.getMessage());
            return false;
        }
    }

    @Override
    public void onlineNotice(VehicleOnLineDto vehicleOnLineDto) {
        try {
            if (vehicleOnLineDto == null) {
                return;
            }
            log.info("Kafka 消费者 车辆在线提醒 ：{}", JSONObject.toJSON(vehicleOnLineDto));

            if (vehicleOnLineDto == null) {
                log.error("Kafka 消费者 车辆在线提醒 数据：{}",
                        vehicleOnLineDto);
                return;
            }

            if (vehicleOnLineDto.getStatus()) {
                iVehicleBaseRemoteService.onlineRemind(vehicleOnLineDto.getVin());
            }
        } catch (Exception e) {
            log.error("Kafka 消费者 车辆在线提醒 处理异常", e);
        }
    }

}
